package com.doge.calcalbum.business.album.video

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.doge.calcalbum.R
import com.doge.calcalbum.business.ITEM_MORE
import com.doge.calcalbum.core.base.adapters.AdapterCallback
import com.doge.calcalbum.core.base.adapters.ITEM_CLICKED
import com.doge.calcalbum.core.base.adapters.ITEM_LONG_CLICKED
import com.doge.calcalbum.databinding.CellAlbumVideoBinding
import com.doge.calcalbum.db.model.Album
import com.doge.calcalbum.utils.*

class AlbumVideoViewHolder(
    parent: ViewGroup,
    callback: AdapterCallback,
    private val binding: CellAlbumVideoBinding = CellAlbumVideoBinding.inflate(
        LayoutInflater.from(parent.context),
        parent,
        false
    ),
) : RecyclerView.ViewHolder(binding.root) {
    init {
        itemView.setOnClickCallback {
            callback.invoke(ITEM_CLICKED, itemView.tag)
        }
        itemView.setOnLongClickListener {
            callback.invoke(ITEM_LONG_CLICKED, itemView.tag)
            true
        }
        binding.layMore.setOnClickCallback {
            callback.invoke(ITEM_MORE, itemView.tag)
        }
    }

    fun bind(item: Album) {
        itemView.tag = item
        binding.count.text = String.format(getString(R.string.xxx_video_count), item.count.toString())
        binding.title.text = item.title

        if (item.cover.isNullOrEmpty()) {
            binding.cover.setImageDrawable(ColorDrawable(getColor("#e5e5e5")))
            binding.videoImage.setBackgroundColor(Color.TRANSPARENT)
            binding.videoImage.setImageResource(R.mipmap.video_cover_empty)
        } else {
            binding.cover.load(
                item.cover,
                placeHolder = R.mipmap.album_video_empty,
                transformation = RoundedCorners4
            )
            binding.videoImage.setBackgroundColor(getColor("#7f000000"))
            binding.videoImage.setImageResource(R.mipmap.video_paused)
        }

    }
}